/*
 *  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.material.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.MaterialSupplierException;
import com.pitop.material.dao.MaterialSupplierDAO;
import com.pitop.material.entity.MaterialSupplier;
import com.pitop.material.entity.MaterialSupplierState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("materialSupplierSV")
@Slf4j
public class MaterialSupplierSVImpl extends BaseSVImpl<MaterialSupplier, Long> implements MaterialSupplierSV {

    @Autowired
    private MaterialSupplierDAO materialSupplierDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return materialSupplierDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(MaterialSupplier entity) throws BaseException {
        entity.setState(MaterialSupplierState.Enable.name());
        super.save(entity);
    }

    /**
     * 加载一个对象MaterialSupplier
     *
     * @param id * @param suppierCode 供应商编码* @param materialCode 物料编码
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier load(Long id, String suppierCode, String materialCode) {
        if (id == null && suppierCode == null && materialCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("suppierCode", suppierCode);
        param.put("materialCode", materialCode);
        return materialSupplierDAO.load(param);
    }

    /**
     * 加载一个对象MaterialSupplier 通过id
     *
     * @param id
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier loadById(Long id) {
        if (id == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialSupplierDAO.loadById(id);
    }

    /**
     * 加载一个对象MaterialSupplier 通过suppierCode
     *
     * @param suppierCode 供应商编码
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier loadBySuppierCode(String suppierCode) {
        if (suppierCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialSupplierDAO.loadBySuppierCode(suppierCode);
    }

    /**
     * 加载一个对象MaterialSupplier 通过materialCode
     *
     * @param materialCode 物料编码
     * @return MaterialSupplier
     */
    @Override
    public MaterialSupplier loadByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return materialSupplierDAO.loadByMaterialCode(materialCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 MaterialSupplier 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, MaterialSupplierState newState, MaterialSupplierState... oldStates) {
        if (id == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialSupplierDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键suppierCode,oldStates 共同更新 MaterialSupplier 的状态到newState状态
     *
     * @param suppierCode 供应商编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateBySuppierCode(String suppierCode, MaterialSupplierState newState, MaterialSupplierState... oldStates) {
        if (suppierCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialSupplierDAO.updateStateBySuppierCode(suppierCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键materialCode,oldStates 共同更新 MaterialSupplier 的状态到newState状态
     *
     * @param materialCode 物料编码
     * @param newState     新状态
     * @param oldStates    旧状态集合
     */
    @Override
    public void updateStateByMaterialCode(String materialCode, MaterialSupplierState newState, MaterialSupplierState... oldStates) {
        if (materialCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        materialSupplierDAO.updateStateByMaterialCode(materialCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 MaterialSupplier 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, MaterialSupplierState state) {
        if (id == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialSupplierDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键suppierCode 更新 MaterialSupplier 的状态到另一个状态
     *
     * @param suppierCode 供应商编码
     * @param state       状态
     */
    @Override
    public void updateBySuppierCode(String suppierCode, MaterialSupplierState state) {
        if (suppierCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialSupplierDAO.updateBySuppierCode(suppierCode, state.name(), new Date());
    }

    /**
     * 根据主键materialCode 更新 MaterialSupplier 的状态到另一个状态
     *
     * @param materialCode 物料编码
     * @param state        状态
     */
    @Override
    public void updateByMaterialCode(String materialCode, MaterialSupplierState state) {
        if (materialCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        materialSupplierDAO.updateByMaterialCode(materialCode, state.name(), new Date());
    }


    /**
     * 删除对象MaterialSupplier
     *
     * @param id * @param suppierCode 供应商编码* @param materialCode 物料编码
     * @return MaterialSupplier
     */
    @Override
    public void delete(Long id, String suppierCode, String materialCode) {
        if (id == null && suppierCode == null && materialCode == null) {
            throw new MaterialSupplierException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("suppierCode", suppierCode);
        param.put("materialCode", materialCode);
        materialSupplierDAO.delete(param);
    }


    /**
     * 查询MaterialSupplier分页
     *
     * @param materialSupplier 对象
     * @param offset           查询开始行
     * @param limit            查询行数
     * @return List<MaterialSupplier>
     */
    @Override
    public List<MaterialSupplier> list(MaterialSupplier materialSupplier, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (materialSupplier != null) {
            map = JSON.parseObject(JSON.toJSONString(materialSupplier, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return materialSupplierDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(MaterialSupplier materialSupplier) {
        Map<String, Object> map = null;
        if (materialSupplier != null) {
            map = JSON.parseObject(JSON.toJSONString(materialSupplier, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return materialSupplierDAO.count(map);
    }

    /**
     * 查询MaterialSupplier分页
     *
     * @param id     * @param suppierCode  供应商编码* @param materialCode  物料编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<MaterialSupplier>
     */
    @Override
    public List<MaterialSupplier> list(Long id, String suppierCode, String materialCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("suppierCode", suppierCode);
        param.put("materialCode", materialCode);
        return materialSupplierDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String suppierCode, String materialCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("suppierCode", suppierCode);
        param.put("materialCode", materialCode);
        return materialSupplierDAO.count(param);
    }

}
